import { getBaseUrl } from '@/services/urlconst';
import { getCompanyID, getPosIDs, getSystemTypeValue, getTenantID, getUserDepID, getUserID, requestFunComponFiex } from '@/utils';
import { Dispatch, useRequest } from 'umi';

let baseUrl = getBaseUrl();

/**
 * 新增快捷键
 * @param addShortcutCallback  调用接口后的回调
 */
const addShortcutService = (addShortcutCallback: (res: any) => void) => {
  const addShortcut = useRequest(
    (payload: any) => {
      let reqUrl = `${baseUrl}/commonController/addLayout`;
      let bodyPayload: any = {
        ...payload,
        userID: getUserID(),
        userPositionID: getPosIDs(),
        layoutType: getTenantID() + '10011000',
        routeURL: '/workmanage/workbench',
      };
      return requestFunComponFiex('POST', reqUrl, bodyPayload);
    },
    {
      manual: true,
      onSuccess: addShortcutCallback,
    },
  );
  return {
    addShortcutRun: addShortcut?.run,
    addShortcutLoading: addShortcut?.loading,
  };
};

/**
 * 编辑快捷键
 * @param editShortcutCallback  调用接口后的回调
 */
const editShortcutService = (editShortcutCallback: (res: any) => void) => {
  const editShortcut = useRequest(
    (payload: Record<string, any>) => {
      let reqUrl = `${baseUrl}/commonController/editLayout`;
      let bodyPayload: any = {
        ...payload,
        userID: getUserID(),
        userPositionID: getPosIDs(),
        layoutType: getTenantID() + '10011000',
        routeURL: '/workmanage/workbench',
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      return requestFunComponFiex('PUT', reqUrl, bodyPayload);
    },
    {
      manual: true,
      onSuccess: editShortcutCallback,
    },
  );
  return {
    editShortcutRun: editShortcut?.run,
    editShortcutLoading: editShortcut?.loading,
  };
};

/**
 * 获取详情
 */
const reloadUserInfoService = (shortcutDispatch: Dispatch<any>) => {
  const reloadUserInfo = useRequest(
    () => {
      let reqUrl = `${baseUrl}/commonController/reloadSelfInfo`;
      const bodyParams = {
        id: getUserID(),
        systemType: getSystemTypeValue(),
      };
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        if (res && res?.msgFlag) {
          shortcutDispatch({
            type: 'baseLogin/updateUserInfo',
            payload: res?.obj,
          });
        }
      },
    },
  );
  return {
    reloadUserInfoRun: reloadUserInfo?.run,
    reloadUserInfoLoading: reloadUserInfo?.loading,
  };
};

export { addShortcutService, reloadUserInfoService, editShortcutService };
